package ModelLayer;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Set;

/**
 * The DVDContainer class
 * 
 * @author (Jan Skrubbeltrang)
 * @version (a version number or a date)
 */
public class DVDContainer
{
    // instance variables
    private static DVDContainer instance;
    private static int index;
    private HashMap<Integer,DVD> dvdContainer;

    /**
     * Constructor for objects of class DVDContainer
     */
    private DVDContainer()
    {
        // initialise instance variables
        dvdContainer = new HashMap<Integer,DVD>();
        index = 0;
    }

    /**
     * Creates an new instance of the DVDContainer object
     *
     * @param getInstance checks if instance is equal to null, if so instance creates new DVDContainer object
     * @return            if instance object is equal to null
     */
    public static DVDContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }

    /**
     * Adds an DVD to the dvdContainer object
     *
     * @param addDVD Adds an DVD to the dvdContainer object
     */
    public DVD addDVD(DVD d)
    {
        index++;
        d.setId(index);
        dvdContainer.put(d.getId(),d);
        return d;
    }

    /**
     * Returns a DVD by input of id from dvdContainer object
     *
     * @param getDVD returns a DVD by input of id integer input from dvdContainer object  
     * @return       if id has been inserted/typed in to the integer field
     */
    public DVD getDVD(int id)
    {
        return dvdContainer.get(id);
    }

    /**
     * Sets an DVD to the dvdContainer object
     *
     * @param setDVD sets an DVD to the dvdContainer object
     */
    public void setDVD(DVD d)
    {
        dvdContainer.put(d.getId(),d);
    }

    /**
     * Remove an DVD by id from the dvdContainer object
     *
     * @param removeDVD removes an DVD by input of id integer input from dvdContainer object  
     */
    public void removeDVD(int id)
    {
        dvdContainer.remove(id);
    }

    /**
     * Lists all the DVDs from the ArrayList DVD
     *
     * @param listAllDVDs by using a for while loop from the ArrayList DVD.
     * @return            when every entry in the for loop has been cycled through
     */
    public ArrayList<DVD> listAllDVDs()
    {
        ArrayList<DVD> dvds = new ArrayList<DVD>();
        Set<Integer> indexes = dvdContainer.keySet(); 
        for(Integer index : indexes)
        {
            DVD d = dvdContainer.get(index);
            dvds.add(d);
        }
        return dvds;
    }
}

